package slicegen;

import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

public class Segment {
	Point3d p1, p2;
	
	public Segment(Point3d point1, Point3d point2)
	{
		p1 = point1;
		p2 = point2;
	}
	
	//Assumes that the segment passes through the plane
	public Point3d planeIntersect(Plane p)
	{		
		Point3d p3p1 = new Point3d(p.point); 
		p3p1.sub(p1);
		
		Point3d p2p1 = new Point3d(p2);
		p2p1.sub(p1);
		
		double numerator = p.normal.dot(new Vector3d(p3p1));
		double denomenator = p.normal.dot(new Vector3d(p2p1));
		
		if (denomenator == 0)
		{
		//	System.err.println("Facet parallel to z-axis");
			return null;
		}
		
		//intersection = p1 + (num/denom)**p2-p1)
		Point3d intersection = new Point3d(p2p1);
		intersection.scaleAdd((numerator / denomenator), p1);
		
		return intersection;
	}
    
    public double length()
    {
        return p1.distance(p2);
    }
}
